
global tmp "/tmp"
global dta "/users/andreas/dropbox/work/patstat_data"
cd /users/andreas/dropbox/work/patstat/data


* ----------------------------------------------
* Preliminaries
* ----------------------------------------------
use $dta/tls906/tls906_all, clear
keep person_id person_ctry_code hrm_l2_id han_id
* Identify headquarter as the most frequent country of residence
replace person_ctry_code = trim(person_ctry_code)
egen headq=mode( person_ctry_code), by( hrm_l2_id ) minmode
save $tmp/906, replace

* ----------------------------------------------
* Find relevant patents
* ----------------------------------------------
use $dta/tls201/tls201, clear
gen y = substr(appln_filing_date,1,4)
destring y, replace
drop if y<1965 | y>2009
keep appln_id appln_auth y
save $tmp/patents, replace

* ----------------------------------------------
* Find all applicants of relevant patents
* (note: potentially many applicants per patent)
* ----------------------------------------------
use $dta/tls207/tls207, clear
keep if applt_seq_nr>0
merge m:1 appln_id using $tmp/patents, keep(match) 
drop _merge

* Find harmonized name
merge m:1 person_id using $tmp/906, keep(match) 
drop _merge

keep hrm headq appln_auth appln_id y
by hrm_l2_id headq appln_auth appln_id , sort: generate nvals = _n == 1
drop if nvals==0
// drop duplicate appln_id for identical combinations of hrm_l2_id headq appln_auth

save pat_hldr_allperiod, replace

* ----------------------------------------------
* Calculate weights
* ----------------------------------------------
use pat_hldr_allperiod_2, clear
drop if headq==""
drop if headq==appln_auth  // In the trade data, we don't have purchases from yourself, do the same for patent data
collapse (count) p=appln_id, by(headq appln_auth y)
save patenttrade2, replace

* ----------------------------------------------
* Combine with trade data
* ----------------------------------------------

use "/Users/andreas/Dropbox/Data/CEPII/Gravity/col_regfile09.dta", clear
drop iso_o iso_d
ren iso2_d appln_auth
ren iso2_o headq
ren year y
merge 1:1 appln_auth headq y using patenttrade2
*bys y: tab _merge

gen lnT = log(flow)
gen lnP = log(p)
gen lndist = log(distw)
gen lngdpo = log(gdp_o)
gen lngdpd = log(gdp_d)

* Plots

label var lnT "Trade, mill USD (logs)"
label var lnP "Number of patents (logs)"
lpoly lnT lnP if y==2000, scheme(lean1) ci legend(off) title("")
reg lnT lnP if y==2000
graph export "../graph/bilateral_patents_gravity.eps", as(eps) preview(on) replace
graph save "../graph/bilateral_patents_gravity.gph", replace


log using ../logs/Appendix_Fig7_Table9, replace
di "********************* Appendix Figure 7 and Table 9 **********************"
* Share of imports
capture drop tmp ww wwT
egen tmp = sum(p), by(appln y)
gen ww = p/tmp
capture drop tmp
egen tmp = sum(flow), by(appln y) 
gen wwT = flow/tmp
label var ww "Import share, patents" 
label var wwT "Import share, trade"
scatter ww wwT if appln=="US" & y==2000 & ww>.00001 & wwT>.00001, mlabel(headq) yscale(log) xscale(log) scheme(lean1) xlab(.0001 .001 .01 .1 1, labsize(12pt)) ylab(.0001 .001 .01 .1 1, labsize(12pt)) name(b1, replace) subtitle("US", size(12pt)) xtitle(, size(12pt)) ytitle(, size(12pt))
scatter ww wwT if appln=="DE" & y==2000 & ww>.00001 & wwT>.00001, mlabel(headq) yscale(log) xscale(log) scheme(lean1) xlab(.0001 .001 .01 .1 1, labsize(12pt)) ylab(.0001 .001 .01 .1 1, labsize(12pt)) name(b2, replace) subtitle("Germany", size(12pt)) xtitle(, size(12pt)) ytitle(, size(12pt))
scatter ww wwT if appln=="JP" & y==2000 & ww>.00001 & wwT>.00001, mlabel(headq) yscale(log) xscale(log) scheme(lean1) xlab(.0001 .001 .01 .1 1, labsize(12pt)) ylab(.0001 .001 .01 .1 1, labsize(12pt)) name(b3, replace) subtitle("Japan", size(12pt)) xtitle(, size(12pt)) ytitle(, size(12pt))
scatter ww wwT if appln=="GB" & y==2000 & ww>.00001 & wwT>.00001, mlabel(headq) yscale(log) xscale(log) scheme(lean1) xlab(.0001 .001 .01 .1 1, labsize(12pt)) ylab(.0001 .001 .01 .1 1, labsize(12pt)) name(b4, replace) subtitle("Great Britain", size(12pt)) xtitle(, size(12pt)) ytitle(, size(12pt))
gr combine b1 b2 b3 b4, scheme(lean1)
graph export "../graph/import_shares_patents_trade.eps", as(eps) preview(on) replace
graph save "../graph/import_shares_patents_trade.gph", replace

* Regressions
egen id = group(appln_auth headq)

reg lnP lndist lngdpo lngdpd if y==2000, vce(robust)
reghdfe lnP lngdpo lngdpd if y>=1965 & y<=2006, absorb(id y) vce(robust)

reg lnT lndist lngdpo lngdpd if y==2000, vce(robust)
reghdfe lnT lngdpo lngdpd if y>=1965 & y<=2006, absorb(id y) vce(robust)

reg lnP lnT if y==2000, vce(robust)
reghdfe lnP lnT if y>=1965 & y<=2006, absorb(id y) vce(robust)

log close